package cn.biplam.back.bean.systemManage.privi;

import java.io.Serializable;
import java.util.ArrayList;

import cn.biplam.common.bean.systemManage.privi.PriviMangeBean;
import cn.biplam.common.bean.tree.IndexTableSimple;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.RolePriviOptCode;
import cn.biplam.common.struc.SysStruc.PriviStruc;
import cn.biplam.common.struc.SysStruc.RolePriviStruc;
import cn.biplam.common.struc.SysStruc.Role_Privi_Flag;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 *  权限管理操作类
 * @author XiaoRuxing Dec 10, 20094:56:09 PM
 * @version 1.0
 */
public class PriviManageBeanImp extends PriviMangeBean implements Serializable{

	/**  */
	private static final long serialVersionUID = 8048329418732L;

	public PriviManageBeanImp(String userId){
		this.userId = userId;
	}

	/**
	 *  创建权限
	 * @param parentId     父亲节点ID
	 * @param operatorType 父亲节点操作类型
	 * @param priviName    权限名称
	 * @param priviDesc    权限描述
	 * @param getSql       是否返回SQL
	 * @return 根据getSql返回<BR>
	 *         getSql:true 返回创建权限SQL,权限ID(OperatorResult.getLastRowId方法获取) 权限排序号(OperatorResult.getValue方法获取)<BR>
	 *         getSql:false 执行创建权限,返回是否创建成功<BR>
	 * @throws ErrorMessage 
	 */
	protected OperatorResult insertPrivi(String parentId, String operatorType,
			String priviName, 
			String priviDesc,
			String nodeType,
			boolean getSql) throws ErrorMessage{
		ArrayList<String> sql = new ArrayList<String>();
		
		//1.插入权限到权限索引表
		IndexTableSimple index = IndexTableSimple.getNewInstance(userId, PriviStruc.Table_Name);
		this.result = index.insertFirst(operatorType, parentId, null, priviName, null, priviDesc, nodeType, true);
		sql.addAll(result.getSqls());
		
		//2.级联操作角色权限表
		//1.1 查询当前权限的直接上级权限
			String parentPrivi = parentId;
		//1.2 查询拥有当前权限的直接上级权限(完全权限)的角色
			DataFilter filter = new DataFilter();
			filter.put(RolePriviStruc.PRIVI_ID,parentPrivi);
			filter.put(RolePriviStruc.SUPPER_FLAG,Role_Privi_Flag.SUPPER);
			OperatorResult priviRoles = Operator.get(RolePriviOptCode.Query_By_Privi_ID, userId, filter);
		//1.3 授权角色当前权限（完全权限）
			if(TOOLS.exist(priviRoles)){
				String curRoleId;
				RoleManageBeanImp roleMange = new RoleManageBeanImp(userId);
				String curPriviId = this.result.getLastRowId();
				do{
					curRoleId = priviRoles.getValue(RolePriviStruc.ROLE_ID);
					roleMange.grantPriviToRole(sql, curRoleId, curPriviId, Role_Privi_Flag.SUPPER.toString());
				}while(priviRoles.next());
			}
		//返回结果
		if(getSql){
			this.result.setSqls(sql);
			return this.result;
		}else{
			return this.doingDML(sql);
		}
	}

	/**
	 *  删除权限,级联删除角色权限(未级联删除权限的下级权限)<BR>
	 *  级联删除下级权限由上层调用操作删除
	 * @param priviId 权限ID
	 * @param getSql  是否获取SQL 
	 * @return 根据getSql参数返回<BR>
	 *         true:返回删除SQL false:执行删除,返回删除是否成功
	 * @throws ErrorMessage 
	 */
	@Override
	public OperatorResult delPrivi(String priviId, boolean getSql) throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();

		//获取当前权限对应的所有角色权限
		DataFilter filter = new DataFilter();
		filter.put(RolePriviStruc.PRIVI_ID,priviId);
		OperatorResult priviRoles = Operator.get(RolePriviOptCode.Query_By_Privi_ID, userId, filter);
		//逐个删除角色权限
		if(TOOLS.exist(priviRoles)){
			do{
				new RoleManageBeanImp(userId).
					delRolePriviNoChildPrivi(priviRoles.getValue(RolePriviStruc.ID), sql);
			}while(priviRoles.next());
		}
		//如果当前权限的父亲权限与角色的关系为非完全拥有关系,且只有角色不含当前权限的兄弟权限,删除父亲权限与角色的非完全拥有关系
		//TODO
		
		IndexTableSimple index = IndexTableSimple.getNewInstance(userId, PriviStruc.Table_Name);
		//删除权限表中权限
		sql.addAll(index.delete(priviId, true).getSqls());
		
		//返回结果
		if(getSql){
			this.setSql(sql);
			return this.result;
		}else{
			return this.doingDML(sql);
		}
	}

}
